package e2q.security.web.manage.message;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import e2q.di.web.util.MvcUtil;
import e2q.security.domain.E2qSmApplication;
import e2q.security.domain.E2qSmRole;
import e2q.security.domain.E2qSmUser;
import e2q.security.domain.E2qSmUserContactGroup;
import e2q.security.domain.E2qSmUserRole;
import e2q.security.domain.E2qSmUserSms;
import e2q.security.web.attributes.PageInfo;
import e2q.security.web.common.SecurityConst;
import e2q.security.web.util.DESPlus;
import e2q.security.web.util.SmsUtil;
import e2q.web.json.JsonArray;
import e2q.web.orm.ICommonDao;

@Controller
public class UserGroupController {
	@Autowired
	@Qualifier("commonDAOProxy")
	private ICommonDao dao;
	
	private Long applicationID;
	private DESPlus desPlus=new DESPlus();
	@RequestMapping("/sms/saveOrUpdateGroup.do")
	public ModelAndView saveOrUpdateGroup(HttpSession session,@RequestParam("loginIds")	String ucgValue,
			@RequestParam("ucgNotes") String ucgNotes,@RequestParam("ucgName") String ucgName,@RequestParam("id") Long id) {

		E2qSmUser user = null;
		List<?> users = dao
		.find(
				"from E2qSmUser e where e.userLoginId=? and userState=? ",
				new Object[] { session.getAttribute("edu.yale.its.tp.cas.client.filter.user"),
						E2qSmUser.USER_STATE_NORMAL });
		if (users.size() > 0) {
			user = (E2qSmUser) users.get(0);
		}
		JSONObject model = new JSONObject();
		if(user!=null){
			if(id!=null){
				E2qSmUserContactGroup e2qSmUserContactGroup=(E2qSmUserContactGroup)dao.find("from E2qSmUserContactGroup where id=?", new Object[]{id}).get(0);
				e2qSmUserContactGroup.setE2qSmUser(user);
				e2qSmUserContactGroup.setUcgName(ucgName);
				e2qSmUserContactGroup.setUcgNotes(ucgNotes);
				e2qSmUserContactGroup.setUcgValue(ucgValue);
				e2qSmUserContactGroup.setUcgState(E2qSmUserContactGroup.GROUP_STATE_NORMAL);
				dao.update(e2qSmUserContactGroup);
			}else{
				E2qSmUserContactGroup e2qSmUserContactGroup=new E2qSmUserContactGroup();
				e2qSmUserContactGroup.setE2qSmUser(user);
				e2qSmUserContactGroup.setUcgName(ucgName);
				e2qSmUserContactGroup.setUcgNotes(ucgNotes);
				e2qSmUserContactGroup.setUcgValue(ucgValue);
				e2qSmUserContactGroup.setUcgState(E2qSmUserContactGroup.GROUP_STATE_NORMAL);
				dao.insert(e2qSmUserContactGroup);
			}
			model.element("success", true);
		}
		

		return MvcUtil.jsonObjectModelAndView(model);
	}
	

	@RequestMapping("/sms/groupUsers.do")
	public ModelAndView groupUsers(@RequestParam("node")	String id) {		
		JSONArray model = new JSONArray();
		
		String[] ids=id.split(",");
//		JSONArray data = new JSONArray();
		if(ids.length>0&&!id.equals("")){
			for(String uid:ids){
				E2qSmUserContactGroup e2qSmUserContactGroup=(E2qSmUserContactGroup)dao.find("from E2qSmUserContactGroup where id=?", new Object[]{Long.parseLong(uid)}).get(0);
				String ucgvalue=e2qSmUserContactGroup.getUcgValue();
				String [] values=null;
				if(ucgvalue!=null&&!ucgvalue.equals(""))
				{
					 values=ucgvalue.split(",");
				}
				if(values!=null){
				for (String userLoginId : values) {
					
					E2qSmUser field = null;
					List<E2qSmUser> tempList=dao.find("from E2qSmUser where userLoginId=?", new Object[]{userLoginId});
					if(tempList.size()>0){
						field=tempList.get(0);
						if (field!=null) {
							JSONObject item = new JSONObject();
							item.element("id", field.getId());
							item.element("loginId", MvcUtil.toJsonString(field
									.getUserLoginId()));
							item.element("text", MvcUtil.toJsonString(field
									.getUserRealName()));
							item.element("userDepartmentId", MvcUtil.toJsonString(field
									.getE2qSmDepartment().getId()));
							item.element("e2qSmDepartment", MvcUtil.toJsonString(field
									.getE2qSmDepartment().getId()));
							item.element("e2qSmDepartmentName", MvcUtil.toJsonString(field
									.getE2qSmDepartment().getDepaName()));
							item.element("userMobile", MvcUtil.toJsonString(desPlus.encrypt(field
									.getUserMobile())));
							item.element("leaf", true);
							model.add(item);
						}
					}
				}

			}
			}
		}				
		return MvcUtil.jsonArrayModelAndView(model);

	}
	
	
	@RequestMapping("/sms/groupList.do")
	public ModelAndView groupList(HttpSession session){
		E2qSmUser user;
		List<Object> treeNodes = null;
		JSONArray model = new JSONArray();
		if (session.getAttribute("edu.yale.its.tp.cas.client.filter.user") != null) {
			treeNodes = dao.find("from E2qSmUser t where t.userLoginId = ? and t.userState <> ?"
					,new Object[]{session.getAttribute("edu.yale.its.tp.cas.client.filter.user").toString(), E2qSmUser.USER_STATE_DELETED});
		}
		if(treeNodes.size() > 0){
			user = (E2qSmUser)treeNodes.get(0);
			List<E2qSmUserContactGroup> groups=dao.find("from E2qSmUserContactGroup where e2qSmUser=?", new Object[]{user});
//			model.element("totalCount", groups.size());
			if (groups.size()>0) {
				for(E2qSmUserContactGroup group:groups){
					JSONObject item = new JSONObject();
					
					item.element("id", group.getId());
					item.element("text", MvcUtil.toJsonString(group.getUcgName()));
					item.element("ucgNotes", MvcUtil.toJsonString(group.getUcgNotes()));
					item.element("ucgValue", MvcUtil.toJsonString(group.getUcgValue()));
					item.element("ucgState", MvcUtil.toJsonString(group.getUcgState()));
					if(group.getUcgValue()==null||group.getUcgValue().equals(""))
					{
						item.element("leaf", true);
					}
					model.add(item);
				}
				

//				model.element("success", true);
			}
		}
		return MvcUtil.jsonArrayModelAndView(model);
	}
	
	@RequestMapping("/sms/delGroup.do")
	public ModelAndView delGroup(@RequestParam("ids")	String ids) {
		JSONObject model = new JSONObject();
		String[] idsTemp=ids.split(",");
		if(idsTemp.length>0){
			for(String id: idsTemp){
				E2qSmUserContactGroup group;
				try {
					group = (E2qSmUserContactGroup)dao.load(E2qSmUserContactGroup.class, Long
							.parseLong(id));
					dao.delete(group);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			model.element("success", true);
		}
		
		
		return MvcUtil.jsonObjectModelAndView(model);
	}
	
	
}
